package org.bozo.im.service.protocols.xmpp.stanza.processor.sasl;

import java.io.IOException;
import java.util.ArrayList;

import org.bozo.im.service.Constants;
import org.bozo.im.service.protocols.xmpp.XMPPSession;
import org.bozo.im.service.protocols.xmpp.stanza.Stanza;
import org.bozo.im.service.protocols.xmpp.stanza.processor.StanzaProcessor;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

public class MechanismStanzaProcessor extends StanzaProcessor {

	private static MechanismStanzaProcessor mInstance;

	private MechanismStanzaProcessor() {}

	public static MechanismStanzaProcessor getInstance() {
		if (mInstance == null)
			mInstance = new MechanismStanzaProcessor();
		return mInstance;
	}

	@Override
	public Stanza getStanza() {
		return Stanza.SASL_MECHANISM;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void processStanza(XmlPullParser pParser, XMPPSession pSession, StanzaProcessor pParentProcessor) throws XmlPullParserException, IOException {

		if (pParentProcessor == MechanismsStanzaProcessor.getInstance()) {
			String lMechanism = pParser.nextText();

			Object lProp = pSession.getConfiguration().getProperty("sasl-mechanisms");
			ArrayList<String> lMechanisms;

			if (lProp != null) {
				lMechanisms = (ArrayList<String>)lProp;
			} else {
				lMechanisms = new ArrayList<String>();
				pSession.getConfiguration().putProperty("sasl-mechanisms", lMechanisms);
			}

			// ---------------------------------------------------------------------------------------
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.stanza.processor.sasl.MechanismsStanzaProcessor.processStanza ---------- Adding new mechanism: " + lMechanism);
			// ---------------------------------------------------------------------------------------

			if (lMechanism != null && lMechanism.length() > 0) {
				lMechanisms.add(lMechanism);
			}

		} else {
			// ---------------------------------------------------------------------------------------
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.stanza.processor.sasl.MechanismStanzaProcessor.processStanza ---------- I don't know what to do with this Stanza");
			// ---------------------------------------------------------------------------------------
		}
	}

}
